using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;

using iFinity.DNN.Modules.CacheMaster.Entities;

namespace iFinity.DNN.Modules.CacheMaster
{
	public partial class CacheDetail : PortalModuleBase
    {
        #region controls
        protected Label lblCacheItem;
        private CacheItemInfo _cacheItem;
        private int _timezoneOffset = 0;
        protected HyperLink hlReturn;
        protected LinkButton btnClearCache;
        #endregion
        #region Web Form Designer generated code
        [System.Diagnostics.DebuggerStepThrough]
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
            btnClearCache.Click += new EventHandler(btnClearCache_Click);
        }


        #endregion
        private void Page_Load(object sender, System.EventArgs e)
		{
            try
            {
                string itemCacheKey = Request.QueryString["cacheKey"];
                hlReturn.Text = Localization.GetString("Return.Text", LocalResourceFile);
                hlReturn.NavigateUrl = DotNetNuke.Common.Globals.NavigateURL(this.TabId);
                if (!Page.IsPostBack)
                {
                    _timezoneOffset = this.PortalSettings.TimeZoneOffset;
                    _timezoneOffset += this.UserInfo.Profile.TimeZone;
                    _cacheItem = CacheController.GetCacheItem(itemCacheKey);
                    if (_cacheItem != null)
                    {
                        CacheItemToScreen();
                    }
                    else
                    {
                        lblCacheItem.Text = Localization.GetString("NoItem.Text", LocalResourceFile);
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
		}
        void btnClearCache_Click(object sender, EventArgs e)
        {
            try
            {
                ClearCacheItem();
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        protected void CacheItemToScreen()
        {
            if (_cacheItem != null)
            {
                HtmlGenericControl cacheItemKey = (HtmlGenericControl) this.FindControl("cacheItemKey");
                HtmlGenericControl cacheItemType = (HtmlGenericControl) this.FindControl("cacheItemType");
                HtmlGenericControl cacheItemCreatedDate = (HtmlGenericControl) this.FindControl("cacheItemCreatedDate");
                HtmlGenericControl cacheItemExpiryDate = (HtmlGenericControl) this.FindControl("cacheItemExpiryDate");
                HtmlGenericControl cacheItemSlidingExpiration =
                    (HtmlGenericControl) this.FindControl("cacheItemSlidingExpiration");
                HtmlGenericControl cacheItemCount = (HtmlGenericControl) this.FindControl("cacheItemCount");
                HtmlGenericControl cacheItemContents = (HtmlGenericControl) this.FindControl("cacheItemContents");
                HtmlGenericControl binItemContents = (HtmlGenericControl) this.FindControl("binItemContents");
                string collectionCountrString = Localization.GetString("CollectionCount.Text", LocalResourceFile);
                cacheItemKey.InnerText = _cacheItem.CacheKey;
                cacheItemType.InnerText = _cacheItem.CacheObjectType;
                cacheItemExpiryDate.InnerText = UIController.FormatUTCDate(_timezoneOffset, _cacheItem.UtcExpiryDate,
                                                                           _cacheItem.UtcCreatedDate);
                cacheItemCreatedDate.InnerText = UIController.FormatUTCDate(_timezoneOffset, _cacheItem.UtcCreatedDate);
                cacheItemContents.InnerText = "Xml Serialized Data: \n" + System.Web.HttpUtility.HtmlDecode(_cacheItem.XmlSerializedData);
                if (_cacheItem.BinarySerializable == false || _cacheItem.BinaryDeSerializable == false)
                    binItemContents.Attributes.Add("class", "cacheItemError");

                if (_cacheItem.BinarySerializable)
                {
                    binItemContents.InnerText = "Binary Serializable";
                }
                else
                {

                    binItemContents.InnerText = "Not Binary Serializable";
                }
                if (_cacheItem.BinaryDeSerializable)
                {
                    binItemContents.InnerText += "; Binary Deserializable";
                }
                else
                {

                    binItemContents.InnerText += "; Not Binary Deserializable";
                }

                if (_cacheItem.Count > -1)
                    cacheItemCount.InnerText = string.Format(collectionCountrString, _cacheItem.Count);
                else
                    cacheItemCount.InnerText = Localization.GetString("NotCollection.Text", LocalResourceFile);

                if (_cacheItem.SlidingExpiration.TotalSeconds > 0)
                    cacheItemSlidingExpiration.InnerText = _cacheItem.SlidingExpiration.ToString();
                else
                    cacheItemSlidingExpiration.InnerText = Localization.GetString("NoSlidingExpiration.Text", LocalResourceFile);
            }
            
        }
        protected void ClearCacheItem()
        {
            string itemCacheKey = Request.QueryString["cacheKey"];
            _cacheItem = CacheController.GetCacheItem(itemCacheKey);
            if (_cacheItem != null)
            {
                CacheController.ClearCacheItem(itemCacheKey);
            }
            Response.Redirect(UIController.GetCacheItemUrl(this.TabId, this.ModuleId , itemCacheKey));
        }
        protected void rptCacheItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Header:
                    HtmlGenericControl cacheItemKeyHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemKeyHdr");
                    HtmlGenericControl cacheItemTypeHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemTypeHdr");
                    HtmlGenericControl cacheItemCreatedDateHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemCreatedDateHdr");
                    HtmlGenericControl cacheItemExpiryDateHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemExpiryDateHdr");
                    HtmlGenericControl cacheItemSlidingExpirationHdr = (HtmlGenericControl)e.Item.FindControl("cacheItemSlidingExpirationHdr");

                    cacheItemKeyHdr.InnerText = Localization.GetString("CacheItemKeyHdr.Text", LocalResourceFile);
                    cacheItemTypeHdr.InnerText = Localization.GetString("CacheItemTypeHdr.Text", LocalResourceFile);
                    cacheItemExpiryDateHdr.InnerText = Localization.GetString("CacheItemExpiryDateHdr.Text", LocalResourceFile);
                    cacheItemCreatedDateHdr.InnerText = Localization.GetString("CacheItemCreatedDateHdr.Text", LocalResourceFile);
                    cacheItemSlidingExpirationHdr.InnerText = Localization.GetString("cacheItemSlidingExpirationHdr.Text", LocalResourceFile);
                    break;

                case ListItemType.Item:
                case ListItemType.AlternatingItem:
            
                    CacheItemInfo cacheItem = (CacheItemInfo)e.Item.DataItem;
                    
                    
                    break;
            }
        }
 	}
}